// pages/Index/cashOutGoods/cashOutGoods.js
const controller = require("../../controllers/Controller").controller


Page({

  /**
   * 页面的初始数据
   */
  data: {
    code:'',
    accountCouponBagId: '',
    type: '',
    detail:{},
    canWriteOff: false,
    shopMsg: {
      shopImg: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20201217/789143768694521856.png",
      shopName: "XX生活餐饮",
      businessTime: "06:00-18:30",
      rovinceName: "XX生活餐饮",
      cityName: "XX生活餐饮",
      districtName: "XX生活餐饮",
      address: "XX生活餐饮",
      id: "31",
      customer: "15874764690",
    },
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // scene: code(核销码),accountCouponBagId(用户持有活动id),
    // type(0一卡通1礼包(废弃)2店铺爆款3友米拼团4限时抵扣券5引流包)
    let scene = decodeURIComponent(options.scene);
    this.setData({
      code: scene.split(",")[0],
      accountCouponBagId: scene.split(",")[1],
      type: scene.split(",")[2],
    })
    this.getGoodsBagDetail(this.data.accountCouponBagId)
    this.canWriteOffHot(this.data.code)
  },

  isToken(){
    const shopId = wx.getStorageSync('token')
    if(!shopId){
      wx.showToast({
        title: '请登录后重新扫描',
        icon:'error'
      })
      setTimeout(()=>{
        wx.redirectTo({
          url: '/pages/login/index',
        })
      },1800)
    }
  },

  toIndexPage() {
    wx.redirectTo({
      url: '/pages/Index/Index',
    })
  },

  phone(e) {
    let phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  //用户持有券详情(爆品)
  getGoodsBagDetail(accountCouponBagId) {
    controller.couponBagDetail({
      accountCouponBagId
    }).then(res => {
      if (res.data.effectiveStartTime) {
        res.data.effectiveStartTime = res.data.effectiveStartTime.split(" ")[0].replace(/-/g,'.')
        res.data.effectiveEndTime = res.data.effectiveEndTime.split(" ")[0].replace(/-/g,'.')
      }
      this.setData({
        detail:res.data,
        shopMsg: res.data.shop,
      })
    this.myShopIdentity()
    })
  },
  //查看店铺爆款核销码是否核销(爆品)
  canWriteOffHot(code) {
    controller.canWriteOffHot({
      code
    }).then(res => {
      this.setData({
        canWriteOff: res.data
      })
    })
  },
  //店铺信息
  myShopIdentity() {
    const value = {
      shopId: this.data.shopMsg.id,
    }
    controller.myShopIdentity(value).then(res => {
      wx.setStorageSync('adminName', res.data.nick)
      wx.setStorageSync('power', res.data.power)
      wx.setStorageSync('shopAccountId', res.data.id)
      wx.setStorageSync('diantype', res.data.type)
      wx.setStorageSync('shopId', res.data.shopId)
    })
  },
  writeOff() {
    if (!this.data.canWriteOff) return

    const value = {
      code: this.data.code,
      shopId: wx.getStorageSync('shopId'),
    }
    controller.writeOffShopHot(value).then(res => {
      this.setData({
        canWriteOff: false
      })
      wx.showToast({
        title: '核销成功',
        icon:'none'
      })
      setTimeout(()=>{
        this.toIndexPage()
      },1800)
    })

  }
})